class Solution:
    def subdomainVisits(self, cpdomains: list) -> list:
        domainCounter = {}
        for cpdomain in cpdomains:
            cpdomainSeg = cpdomain.split(" ")
            count, domain = int(cpdomainSeg[0]), cpdomainSeg[1]
            domainSeg = domain.split(".")
            subDomain = ""
            while domainSeg:
                subDomain = domainSeg.pop() + subDomain
                if subDomain not in domainCounter:
                    domainCounter[subDomain] = 0
                domainCounter[subDomain] += count
                subDomain = '.' + subDomain

        res = []
        for d, c in domainCounter.items():
            res.append(str(c) + " " + d)

        return res
